import React from "react";
import depsAreSame from "../utils/depsAreSame";

/**
 * https://ahooks.js.org/zh-CN/hooks/use-creation
 * useCreation 是 useMemo 或 useRef 的替代品。在初始化执行，update 不执行
 */
function useCreation(factory, deps) {
  const { current } = React.useRef({
    deps,
    obj: undefined,
    initialized: false, //是否初始化过
  });
  if (current.initialized === false || !depsAreSame(current.deps, deps)) {
    current.deps = deps;
    current.obj = factory();
    current.initialized = true;
  }
  return current.obj;
}
export default useCreation;
